const App = Vue.createApp({
    data() {
        return {
            Data: [], page: 1, ListRule: [], limit: 20, name: '', cid: $("#AppHis").attr('cid'), type: -1, count: 0,
            Money: 0, Rules: [], RulesType: 1, state: -1, sqid: $("#AppHis").attr('sqid')
        }
    }, watch: {
        state(val, ts) {
            if (val === ts) return false;
            this.initialization(this.name);
        }
    }, methods: {
        SortSet(gid, _this) {
            let value = _this.value - 0;
            let is = layer.msg('加载中，请稍后...', { icon: 16, time: 9999999 });
            $.ajax({
                type: "POST", url: './main.php?act=GoodsSortSet', data: {
                    gid: gid, sort: value,
                }, dataType: "json", success: function (res) {
                    layer.close(is);
                    if (res.code == 1) {
                        App.initialization();
                    } else {
                        layer.alert(res.msg, {
                            icon: 2
                        });
                    }
                }
            });
        },
        RuleName(id, type = 1) {
            let Data = false;
            try {
                for (const listRuleKey in App.ListRule) {
                    if (App.ListRule[listRuleKey].id == id) {
                        Data = App.ListRule[listRuleKey];
                    }
                }
            } catch (e) {
                if (type === 2) {
                    return false;
                }
                return '此规则已失效';
            }

            if (!Data) {
                if (type === 2) {
                    return false;
                }
                return '此规则已失效';
            }

            if (type === 2) {
                return Data;
            }

            return Data.name;
        },
        RuleNameTips(id, money, name) {
            let Data = this.RuleName(id, 2);
            if (!Data) {
                layer.msg('此商品加价规则已失效！', { icon: 2 });
                return false;
            }

            this.Money = money - 0;
            this.Rules = Data.json;
            this.RulesType = Data.type - 0;

            mdui.dialog({
                title: Data.name + '[' + name + ']',
                content: `
<div style="min-height: 60vh;border: solid darkgrey;padding: 5px;"><table class="layui-table" lay-size="sm">
    <colgroup>
        <col width="50">
  </colgroup>
  <thead>
    <tr>
      <th>等级</th>
      <th>余额加价 / 售价</th>
      <th>积分加价 / 兑换价</th>
    </tr>
  </thead>
  <tbody id="ListRule"></tbody>
  </table></div>`,
                modal: true,
                destroyOnClosed: true,
                closeOnEsc: false,
                history: false,
                buttons: [{
                    text: '关闭',
                }],
                onOpened: function () {
                    App.RenderingRul(Data);
                }
            })
        },
        RenderingRul() {
            let content = ``;
            for (const key in App.Rules) {
                try {
                    data = App.Rules[key];
                } catch (e) {
                    continue;
                }
                if (App.RulesType == 1) {
                    price = (App.Money + (data.price - 0)).toFixed(2);
                    profits = (App.Money + (data.profits - 0)).toFixed(0);
                } else {
                    price = (App.Money + (App.Money * (data.price / 100))).toFixed(2);
                    profits = (App.Money + (App.Money * (data.profits / 100))).toFixed(0);
                }
                content += `<tr>
      <td>V` + (key - 0 + 1) + `</td>
      <td>加价：` + data.price + (App.RulesType == 1 ? '元' : '%') + `<br>售价：` + price + `元</td>
      <td>加价：` + data.profits + (App.RulesType == 1 ? '个' : '%') + `<br>兑换价：` + profits + `点</td>
    </tr>`;
            }
            $("#ListRule").html(content);
        },
        GoodsCopy(gid) {
            mdui.dialog({
                title: '温馨提示',
                content: '是否要将商品【' + gid + '】复制一份？此功能用于快速添加商品！',
                modal: true,
                history: false,
                buttons: [{
                    text: '关闭',
                }, {
                    text: '确定复制', onClick: function () {
                        let is = layer.msg('加载中，请稍后...', { icon: 16, time: 9999999 });
                        $.ajax({
                            type: "POST", url: 'main.php?act=GoodsCopy', data: {
                                gid: gid
                            }, dataType: "json", success: function (res) {
                                layer.close(is);
                                if (res.code == 1) {
                                    layer.alert(res.msg, {
                                        icon: 1, btn1: function () {
                                            App.initialization();
                                        }
                                    });
                                } else {
                                    layer.alert(res.msg, {
                                        icon: 2
                                    });
                                }
                            }, error: function () {
                                layer.msg('服务器异常！');
                            }
                        });
                    }
                }]
            });
        }, help() {
            const content = `
<div class="mdui-table-fluid">
  <table class="mdui-table mdui-table-hoverable">
    <thead>
      <tr>
        <th>功能</th>
        <th>说明</th>
      </tr>
    </thead>
    <tbody>
      <tr>
        <td style="white-space: nowrap;color:red">搜索商品</td>
        <td>点击 <a title="搜索商品" href="javascript:App.SearchGoods();" class="badge badge-danger"><i class="layui-icon layui-icon-search"></i></a> 图标即可搜索商品,可填写商品关键词!</td>
      </tr>
      <tr>
        <td style="white-space: nowrap;color:red">按分类搜索商品</td>
        <td>
        点击所属分类下的 <a href="javascript:void;">( xxx )</a> 即可取出此分类下的全部商品
        </td>
      </tr>
      <tr>
        <td style="white-space: nowrap;color:red">初始化列表</td>
        <td>
        可点击 <a title="初始化" href="javascript:App.initialization();" class="badge badge-warning"><i class="layui-icon layui-icon-refresh-3"></i></a> 图标初始化商品列表,重新载入商品
        </td>
      </tr>
      <tr>
        <td style="white-space: nowrap;color:red">批量设置商品</td>
        <td>
        可点击 <a title="批量设置" href="javascript:App.BatchEditor()" class="badge badge-success"><i class="layui-icon layui-icon-set"></i></a> 图标,根据按钮批量操作商品!
        </td>
      </tr>
      <tr>
        <td style="white-space: nowrap;color:red">每页商品显示数量调整</td>
        <td>
        可滑倒页面最底部,点击下一页后面的页码选择,按需选择即可!最多可同时显示1万条商品(小心服务器扛不住~)!
        </td>
      </tr>
      <tr>
        <td style="white-space: nowrap;color:red">查看商品基础信息</td>
        <td>
        可点击商品名称,查看基础信息,可以获取商品的购买链接,商品海报图等!
        </td>
      </tr>
      <tr>
        <td style="white-space: nowrap;color:red">卡密商品补卡</td>
        <td>
        可点击库存旁边的<a href="javascript:void" title="点击补卡" class="badge badge-warning-lighten">自动发卡</a> 补卡或管理卡密!
        </td>
      </tr>
      <tr>
        <td style="white-space: nowrap;color:red">删除商品</td>
        <td>
        除了可以使用批量删除功能外,还可以单独点击商品列表最左边的 设置 图标来删除对应商品
        </td>
      </tr>
      <tr>
        <td style="white-space: nowrap;color:red">商品排序</td>
        <td>
        商品首页展示排序是根据商品的排序参数决定的,可以点击排序列的按钮进行调整,或打开导航栏->商品相关->商品管理->商品排序,进行批量设置!
        </td>
      </tr>
      <tr>
        <td style="white-space: nowrap;color:red">商品状态</td>
        <td>
        商品有两种状态,下架或上架,下架的商品无法被他人购买,点击状态名称,可以快速切换商品上下架状态,或点击上方<a title="批量设置" class="badge badge-success"><i class="layui-icon layui-icon-set"></i></a> 图标,批量设置商品状态!
        </td>
      </tr>
      <tr>
        <td style="white-space: nowrap;color:red">备忘录</td>
        <td>
        备忘录可以用来填写一些商品的对接信息,记录等,仅你可见
        </td>
      </tr>
      <tr>
        <td style="white-space: nowrap;color:red">利润比例</td>
        <td>
        即此商品可赚取利润的百分比比例,基于用户等级,如当一个商品成本为100元时,可以在某个等级用户身上赚取50元的利润,可能50元对于此商品来说太多,用户不会购买此商品,用户等级利润又是固定死的,这时就可以通过调整商品的利润比例来进行价格微调!,如设置为50%,则仅从此用户身上赚25元,如果填写200%,则从此用户身上赚100元,50利润的200%,以此类推,当利润比例设置为0%时,则按照成本价出售商品!
        </td>
      </tr>
    </tbody>
  </table>
</div>
                `;
            mdui.dialog({
                title: '帮助说明', content: content, modal: true, history: false, buttons: [{
                    text: '关闭',
                }]
            });
        }, alert(data) {
            const content = `
                <img src="` + data.image[0] + `" style="width: 140px; height: 140px;display:block;margin: auto"> <br>
                商品编号：` + data.gid + `<br>
                每日库存：` + data.quota + ` 份<br>
                每份数量：` + data.quantity + ` 个<br>
                最少购买：` + data.min + ` 份, <b>` + (data.quantity * data.min) + `</b> 个<br>
                最多购买：` + data.max + ` 份, <b>` + (data.quantity * data.max) + `</b> 个<br>
                商品名称：` + data.name + ` <br>
                商品成本：<font color=red>` + data.money + `</font><br>
                商品售价：<font color=red>` + data.price + `</font><br>
                购买链接：<a href='../?mod=route&p=Goods&gid=` + data.gid + `' target='_blank' >` + document.domain + `/?mod=route&p=Goods&gid=` + data.gid + `</a><br>
                
                `;
            mdui.dialog({
                title: '商品信息', content: content, modal: true, history: false, buttons: [{
                    text: '关闭',
                }]
            });
        }, GoodsDelete(id, name) {
            layer.open({
                title: '操作确认',
                content: '你确定要删除这些数据吗？',
                icon: 3,
                btn: ['确定', '取消'],
                btn1: function () {
                    let is = layer.msg('加载中，请稍后...', { icon: 16, time: 9999999 });
                    $.ajax({
                        type: "POST", url: './main.php?act=quickDelete', data: {
                            id: id
                        }, dataType: "json", success: function (res) {
                            layer.close(is);
                            if (res.code == 1) {
                                layer.alert(res.msg, {
                                    icon: 1, btn1: function () {
                                        App.initialization();
                                    }
                                });
                            } else {
                                layer.alert(res.msg, {
                                    icon: 2
                                });
                            }
                        }, error: function () {
                            layer.msg('服务器异常！');
                        }
                    });
                }
            })
        }, GoodsStateSet(gid, type, name) {
            let is = layer.msg('加载中，请稍后...', { icon: 16, time: 9999999 });
            $.ajax({
                type: "POST", url: './main.php?act=GoodsStateSet', data: {
                    gid: gid, type: type, name: name
                }, dataType: "json", success: function (res) {
                    layer.close(is);
                    if (res.code == 1) {
                        App.GoodsList();
                    } else {
                        layer.alert(res.msg, {
                            icon: 2
                        });
                    }
                }, error: function () {
                    layer.msg('服务器异常！');
                }
            });
        }, fullOpen(n, m) {
            var result = Math.random() * (m - n) + n;
            while (result == n) {
                result = Math.random() * (m - n) + n;
            }
            return result;
        }, GoodsList() {
            let is = layer.msg('载入中，请稍后...', { icon: 16, time: 9999999 });
            $.ajax({
                type: "POST", url: './main.php?act=quickList', data: {
                    page: App.page, limit: App.limit, docs: App.name
                }, dataType: "json", success: function (res) {
                    layer.close(is);
                    if (res.code == 1) {
                        App.Data = res.data;
                        App.type = 1;
                        App.count = res.data.length;
                    } else {
                        layer.alert(res.msg, {
                            icon: 2
                        });
                    }
                }, error: function () {
                    layer.msg('服务器异常！');
                }
            });
        }, BatchEditor(type = 1) {
            console.log(type);
            const arr = goods.pitch_on();
            if (arr.length === 0) {
                layer.alert('请至少选择一条数据!', { icon: 2 });
                return;
            }

            App.GoodsDelete(arr.join(","));

        }, BatchProductPictureTransfer(arr) { //图片本地化
            //确认框
            layer.confirm('是否要将这' + arr.length + '个商品图片本地化？<hr>当您将商品图片本地化后，其他同行将无法根据图片地址信息找到上游货源，并且再也不会出现图片无法显示的问题。然而，请注意，如果您的主机或服务器带宽较小，可能会导致图片加载缓慢，从而影响用户体验。', {
                title: '商品图片本地化', icon: 3, area: ["300px"], btn: ['确认', '取消'], btn1: function () {
                    let is = layer.msg('操作中，请稍后...', { icon: 16, time: 9999999 });
                    $.ajax({
                        type: "POST", url: './main.php?act=BatchProductPictureTransfer', data: {
                            array: arr
                        }, dataType: "json", success: function (res) {
                            //重新获取商品列表
                            layer.close(is);
                            if (res.code === 1) {
                                layer.alert(res.msg, {
                                    icon: 1, btn1: function () {
                                        App.GoodsList();
                                    }
                                });
                            } else {
                                layer.alert(res.msg, {
                                    icon: 2
                                });
                            }
                        }
                    })
                }
            })
        }, ReplaceProductImages(arr, images = false) { //批量换图
            let value = '';
            if (images) {
                value = images.join("\n");
            }
            layer.prompt({
                title: '批量换图,每行一个图片链接',
                formType: 2,
                value: value
            }, function (value, index, elem) {
                if (value === '') {
                    layer.msg('请输入图片链接！');
                    return;
                }
                let images = value.split("\n");
                if (images.length === 0) {
                    layer.msg('请输入图片链接！');
                    return;
                }
                layer.close(index);
                let is = layer.msg('操作中，请稍后...', { icon: 16, time: 9999999 });
                $.ajax({
                    type: "POST", url: './main.php?act=ReplaceProductImages', data: {
                        array: arr, image: images
                    }, dataType: "json", success: function (res) {
                        //重新获取商品列表
                        layer.close(is);
                        if (res.code === 1) {
                            layer.alert(res.msg, {
                                icon: 1, btn1: function () {
                                    App.GoodsList();
                                }
                            });
                        } else {
                            layer.alert(res.msg, {
                                icon: 2
                            });
                        }
                    }
                })
            });
        }, addQuick() {
            layer.prompt({
                formType: 2, value: this.name, title: '请填写文案内容',
            }, function (value, index) {
                $.post("./main.php?act=quickSave", { docs: value }).then(res => {
                    layer.close(index);
                    if (res.code == 1) {
                        layer.msg(res.msg);
                        App.initialization();
                    } else {
                        layer.msg(res.msg);
                    }
                })
            });
        }, SearchGoods() {
            layer.prompt({
                formType: 2, value: this.name, title: '请输入商品名称关键字',
            }, function (value, index) {
                App.initialization(value);
                layer.close(index);
            });
        }, initialization(name = '') {
            this.page = 1;
            if (name !== '') {
                this.name = name;
            }
            this.type = -1;
            layui.use('laypage', function () {
                var laypage = layui.laypage;
                $.ajax({
                    type: "POST", url: './main.php?act=quickCount', data: {
                        cid: App.cid, sqid: App.sqid, name: name, state: App.state,
                    }, dataType: "json", success: function (res) {
                        if (res.code == 1) {
                            laypage.render({
                                elem: 'Page',
                                count: res.count,
                                theme: '#641ec6',
                                limit: App.limit,
                                limits: [1, 10, 20, 30, 50, 100, 200, 500, 1000, 1500, 2000, 3000, 4000, 5000, 10000],
                                groups: 3,
                                first: '首页',
                                last: '尾页',
                                prev: '上一页',
                                next: '下一页',
                                skip: true,
                                layout: ['count', 'page', 'prev', 'next', 'limit', 'limits'],
                                jump: function (obj) {
                                    App.page = obj.curr;
                                    App.limit = obj.limit;
                                    App.GoodsList();
                                }
                            });
                        } else {
                            layer.alert(res.msg, {
                                icon: 2
                            });
                        }
                    }, error: function () {
                        layer.msg('服务器异常！');
                    }
                });
            });
        }
    },
}).mount('#App');

const goods = {
    note_set: function (gid) {
        $.ajax({
            type: "post", url: "ajax.php?act=goods_content", data: {
                gid: gid, field: 'note'
            }, success: function (data) {
                if (data.code == 1) {
                    layer.prompt({
                        formType: 2,
                        value: data.data,
                        title: '商品' + gid + '备忘录内容,仅你可见',
                        maxlength: 99999999999,
                    }, function (value, index) {
                        $.ajax({
                            type: "post", url: "ajax.php?act=note_set", data: {
                                note: value, gid: gid
                            }, dataType: "json", success: function (data) {
                                if (data.code == 1) {
                                    layer.msg(data.msg, {
                                        icon: 1, time: 500, end: function () {
                                            App.GoodsList();
                                        }
                                    });
                                } else {
                                    layer.msg(data.msg, {
                                        icon: 2
                                    });
                                }
                            }, error: function () {
                                layer.alert('加载失败！');
                            }
                        });
                        layer.close(index);
                    });
                } else {
                    layer.msg(data.msg, {
                        icon: 2
                    });
                }
            }, error: function () {
                layer.alert('加载失败！');
            }
        });
    }, FreightTem: function () {
        var arr = goods.pitch_on();
        if (arr.length == 0) {
            layer.msg('请先选中1个商品！');
            return false;
        }

        var load = layer.load(3);
        $.ajax({
            type: "POST", url: "./ajax.php?act=BatchFreightTem", dataType: "json", success: function (data) {
                layer.close(load);
                if (data.code == 1) {
                    var content = '<div style="width: 100%;" class="layui-elip"><a href="javascript:goods.BatchFreightTem(\'' + arr.join('|') + '\',-1)" style="color: tomato">点击设置</a>：不使用运费模板</div>';
                    $.each(data.data, function (key, val) {
                        content += '<div style="width: 100%;" class="layui-elip"><a href="javascript:goods.BatchFreightTem(\'' + arr.join('|') + '\',' + val.id + ')" style="color: tomato">点击设置</a>：' + val.name + ' | ' + val.id + '</div>';
                    });
                    layer.open({
                        title: '批量设置运费模板', content: content, btn: ['取消'],
                    });
                } else layer.msg(data.msg, {
                    icon: 2
                });
            }, error: function () {
                layer.alert('加载失败！');
            }
        });
    }, BatchClass: function (gid, cid) {
        var arr = gid.split('|');
        layer.alert('是否将这' + arr.length + '个商品移动到分类:' + cid + '里面？', {
            title: '商品批量移动确认!', icon: 3, btn: ['确认', '取消'], btn1: function () {
                goods.ajax_state(6, arr, cid);
            }
        });
    }, BatchRule: function (gid, id) {
        var arr = gid.split('|');
        layer.alert('是否将这' + arr.length + '个商品的加价规则设置为' + id + '？', {
            title: '温馨提示!', icon: 3, btn: ['确认', '取消'], btn1: function () {
                var load = layer.load(3);
                $.ajax({
                    type: "POST", url: "./main.php?act=BatchGoodsRule", data: {
                        array: arr, id: id,
                    }, dataType: "json", success: function (data) {
                        layer.close(load);
                        if (data.code == 1) {
                            layui.use(['table'], function () {
                                App.GoodsList();
                            });
                            layer.alert(data.msg, {
                                icon: 1, title: '批量操作成功'
                            });
                        } else layer.msg(data.msg, {
                            icon: 2
                        });
                    }, error: function () {
                        layer.alert('加载失败！');
                    }
                });
            }
        });
    }, BatchFreightTem: function (gid, id) {
        var gid_arr = gid.split('|');
        layer.alert('是否要设置商品：<br>' + gid + '<br>的运费模板为：' + id + '？', {
            title: '操作确认', icon: 3, btn: ['确定', '取消'], btn1: function (layero, index) {
                //操作
                var load = layer.load(3);
                $.ajax({
                    type: "POST", url: "./ajax.php?act=BatchFreightTemEdit", data: {
                        gid: gid_arr, id: id,
                    }, dataType: "json", success: function (data) {
                        layer.close(load);
                        if (data.code == 1) {
                            layui.use(['table'], function () {
                                App.GoodsList();
                            });
                            layer.alert(data.msg, {
                                icon: 1, title: '批量操作成功'
                            });
                        } else layer.msg(data.msg, {
                            icon: 2
                        });
                    }, error: function () {
                        layer.alert('加载失败！');
                    }
                });
            }
        });
    }, select_id: function (id) {
        if ($('.box_' + id).prop('checked') == true) {
            $('.box_' + id).attr("checked", true);
            $('.box_' + id).prop("checked", true);
        } else {
            $('.box_' + id + ',#list_all').attr('checked', false);
            $('.box_' + id + ',#list_all').prop("checked", false);
        }
    }, select_all: function () {
        if ($('#list_all').prop('checked') == true) {
            $(".list_box,#list_all").attr("checked", true);
            $(".list_box,#list_all").prop("checked", true);
        } else {
            $(".list_box,#list_all").attr('checked', false);
            $(".list_box,#list_all").prop("checked", false);
        }
    }, pitch_on: function () {
        var amount = [];
        $("#table input[type='checkbox']").each(function () {
            if ($(this).is(":checked")) {
                if ($(this).val() != 'true' && $(this).val() != '') amount.push($(this).val());
            }
        });
        return amount;
    }, pitch_ons: function () {
        var amount = [];
        $("#mmda input[type='checkbox']").each(function () {
            if ($(this).is(":checked")) {
                if ($(this).val() != 'true' && $(this).val() != '') amount.push($(this).val());
            }
        });
        return amount;
    }, ajax_state: function (type, arr, cid = -1) {
        App.GoodsDelete(arr.join(","));
    }, RuleDataGet() { //获取加价规则
        $.ajax({
            type: "POST", url: './main.php?act=ListRule', dataType: "json", success: function (res) {
                if (res.code == 1) {
                    App.ListRule = res.data;
                } else {
                    App.ListRule = [];
                }
            }, error: function () {
                layer.msg('服务器异常！');
            }
        });
    },
    SharePoster: function (gid) {
        layer.msg('商品分享海报正在生成中...', {
            icon: 16, time: 9999999
        });
        $.ajax({
            type: "post", url: "../main.php?act=SharePoster", data: {
                gid: gid
            }, dataType: "json", success: function (data) {
                if (data.code == 1) {
                    layer.alert('<img src="' + data.src + '" width=300 heigth=450 />', {
                        area: ['340px', '490px'], title: false, btn: false, shade: [0.8, '#000'], shadeClose: true,
                    })
                } else {
                    layer.msg(data.msg, {
                        icon: 2
                    });
                }
            }, error: function () {
                layer.alert('生成失败！,可能是商品图片地址无法访问');
            }
        });
    },
    PriceSynchro: function (gid) {
        layer.msg('正在同步商品价格中...', {
            icon: 16, time: 9999999
        });
        $.ajax({
            type: "post", url: "../main.php?act=GoodsMonitoring", data: {
                gid: gid
            }, dataType: "json", success: function (data) {
                if (data.code == 1) {
                    layer.alert('同步完成，无变动', {
                        icon: 1
                    });
                } else {
                    layer.alert(data.msg, {
                        icon: 2,
                        end: function () {
                            App.GoodsList();
                        }
                    });
                }
            }, error: function () {
                layer.alert('服务器异常！');
            }
        });
    }
};

App.initialization();
goods.RuleDataGet();

$(document).on('change', 'select[name=GoodsState]', function () {
    App.state = $(this).val();
    App.initialization();
});